package org.cttic.visualization.visualization.util;


import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;

import org.elasticsearch.client.core.CountRequest;
import org.elasticsearch.client.core.CountResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;

import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;


/**
 * @author shkstart
 * @create 2020-05-18 13:53
 */
@Component
public class AggregationUtil {
    // 引入 Ela 连接实列化对象

    @Autowired
    private RestHighLevelClient restHighLevelClient;

    /* 多条件统计
     * @param startTime endTime 起止时间
     * @param startCurareacode  endCurareacode 起止省编码
     * @return
     */
    public String queryCount(String startTime,String endTime,int startCurareacode,int endCurareacode,int gantryType){

        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        // 搜索方式
        // matchAllQuery搜索全部
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        boolQueryBuilder.must(QueryBuilders.rangeQuery("tansTime")
                .gt(startTime)
                .lt(endTime));
        boolQueryBuilder.must(QueryBuilders.rangeQuery("curareacode").gt(startCurareacode).lt(endCurareacode));

        boolQueryBuilder.must(QueryBuilders.termQuery("gantryType",gantryType));
        searchSourceBuilder.query(boolQueryBuilder);

//        searchSourceBuilder.size(0);
        // 设置源字段过虑,第一个参数结果集包括哪些字段，第二个参数表示结果集不包括哪些字段
        //searchSourceBuilder.fetchSource(new String[]{"vehicleNo","tansTime","gantryId"},new String[]{});
        // 向搜索请求对象中设置搜索源
//        searchRequest.source(searchSourceBuilder);
        // 执行搜索,向ES发起http请求
        CountRequest countRequest = new CountRequest("trip_incomplete_202005","trip_info_202005");
        countRequest.types("_doc");

        countRequest.source(searchSourceBuilder);
        CountResponse count = null;
        try {
            count = restHighLevelClient.count(countRequest, RequestOptions.DEFAULT);
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("查询结果："+count.getCount());

        return count.getCount()+"";
    }
    public String queryStationCount(String startTime,String endTime,int startCurareacode,int endCurareacode){
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        // 搜索方式
        // matchAllQuery搜索全部
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        boolQueryBuilder.must(QueryBuilders.rangeQuery("tansTime")
                .gt(startTime)
                .lt(endTime));
        boolQueryBuilder.must(QueryBuilders.rangeQuery("curareacode").gt(startCurareacode).lt(endCurareacode));


        boolQueryBuilder.must(QueryBuilders.termQuery("gantryType",1));
        searchSourceBuilder.query(boolQueryBuilder);

//        searchSourceBuilder.size(0);
        // 设置源字段过虑,第一个参数结果集包括哪些字段，第二个参数表示结果集不包括哪些字段
        //searchSourceBuilder.fetchSource(new String[]{"vehicleNo","tansTime","gantryId"},new String[]{});
        // 向搜索请求对象中设置搜索源
//        searchRequest.source(searchSourceBuilder);
        // 执行搜索,向ES发起http请求
        CountRequest countRequest = new CountRequest("trip_incomplete_202005");
        countRequest.types("_doc");

        countRequest.source(searchSourceBuilder);
        CountResponse count = null;
        try {
            count = restHighLevelClient.count(countRequest, RequestOptions.DEFAULT);
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("查询trip_incomplete_202005结果："+count.getCount());
        CountRequest countRequest2 = new CountRequest("trip_info_202005");
        countRequest.types("_doc");

        countRequest2.source(searchSourceBuilder);
        CountResponse count2 = null;
        try {
            count2 = restHighLevelClient.count(countRequest2, RequestOptions.DEFAULT);
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("查询trip_info_202005结果："+count2.getCount());
        return count.getCount()+"|"+count2.getCount();
    }
}
